<?php
/**
 * @deprecated - 品牌由 customer 权限中管理了！
 *
 * 商品品牌的管理
 * Author: lyun
 * Date: 2018/4/16
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Panx\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\Product\ProductBrandStoreRequest;
use App\Http\Requests\Admin\Panx\Product\ProductBrandUpdateRequest;
use App\Models\Admin\PanxAdmin;
use App\Models\Panx\Brand;
use Illuminate\Http\Request;

class ProductBrandController extends ApiController
{

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $brands = Brand::where('customer_id', Brand::CUSTOMER_ID_PANX)
            ->latest()
            ->paginate($request->per_page ?: 15);

        return $this->response(compact('brands'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  ProductBrandStoreRequest  $request
     * @return \Illuminate\Http\Response
     *
     * @throws \Exception
     */
    public function store(ProductBrandStoreRequest $request)
    {
        // 超管才能操作的
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        $brand = $this->assembleBrand($request);

        $stored = Brand::create($brand);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  ProductBrandUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * @throws \Exception
     */
    public function update(ProductBrandUpdateRequest $request, $id)
    {
        // 超管才能操作的
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var Brand $brand */
        $brand = Brand::where('customer_id', Brand::CUSTOMER_ID_PANX)
            ->find($id);
        $this->checkItemExist($brand, trans('admin.brand'));
        $this->checkUnless($brand->locked, trans('admin.brand_locked'));

        $brand_data = $this->assembleBrand($request);

        $updated = $brand->update($brand_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     *
     * @throws \Exception
     */
    public function destroy($id)
    {
        // 超管才能操作的
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var Brand $brand */
        $brand = Brand::where('customer_id', Brand::CUSTOMER_ID_PANX)
            ->find($id);
        $this->checkItemExist($brand, trans('admin.brand'));
        $this->checkUnless($brand->locked, trans('admin.brand_locked'));
        $this->checkUnless($brand->products()->count(), trans('admin.brand_product'));

        $deleted = $brand->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param ProductBrandStoreRequest|ProductBrandUpdateRequest|Request $request
     * @return array
     */
    private function assembleBrand(Request $request)
    {
        return [
            'name'        => $request->name,
            'url'         => $request->url,
            'logo'        => $request->logo,
            'customer_id' => Brand::CUSTOMER_ID_PANX
        ];
    }
}
